package com.thug.client;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ProviderWs {

	private static final Log LOGGER = LogFactory.getLog(ProviderWs.class);

	private static final int DURATION_MS = 5000;
	private static boolean isSlow = false;

	public List<Product> getProducts(int customerId) throws Exception {

		LOGGER.debug("start");

		try {
			if (isSlow) {
				LOGGER.info("Web Services are slow.");
				Thread.sleep(DURATION_MS);
			} else {
				LOGGER.debug("Web Services are OK.");
			}

			Product product1 = new Product();
			product1.setId(UUID.randomUUID().toString());
			product1.setName(UUID.randomUUID().toString());

			Product product2 = new Product();
			product2.setId(UUID.randomUUID().toString());
			product2.setName(UUID.randomUUID().toString());

			List<Product> products = new ArrayList<Product>();

			products.add(product1);
			products.add(product2);

			return products;

		} finally {

			LOGGER.debug("end");
		}
	}

	public static void setIsSlow(boolean isFallingDown) {
		ProviderWs.isSlow = isFallingDown;
	}
}
